package voice.main;

import object.Dialogue;
import thread.RetrieveAIMLTask;
import util.TaskHelper;
import voice.main.R;
import adapter.ConversationArrayAdapter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class TextChatFragment extends ChatFragment implements OnClickListener {  
    
    public TextChatFragment() { 
    }  

    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
    	rootView = inflater.inflate(R.layout.text_chat, container, false);  
    	adapter = new ConversationArrayAdapter(getActivity().getApplicationContext(), R.layout.list_item_conversation);
    	((ListView) rootView.findViewById(R.id.conversationListView)).setAdapter(adapter);  
    	
    	ImageButton b = (ImageButton) rootView.findViewById(R.id.commandButton);
        b.setOnClickListener(this);
        return rootView;  
    }  
    

	public void onClick(View v) {
		switch (v.getId()) {
		    case R.id.commandButton:
		    	if (!TaskHelper.isNetworkConnected(getActivity().getApplicationContext())) 
		    	{
		    		Toast.makeText(getActivity().getApplicationContext(), "No internet connection is found.", Toast.LENGTH_SHORT).show();
		    		return;
		    	}
		    	EditText commandText = (EditText)rootView.findViewById(R.id.commandText);
		    	String command = commandText.getText().toString();
		    	commandText.setText("");
		    	adapter.add(new Dialogue(true, '"'+ command + '"'));
		    	new RetrieveAIMLTask(this).execute(command);
		    	
		        break;
	    }
	}
} 
